<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="vue3新特性"><meta name="keywords" content="vue3"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>vue3新特性 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E4%B8%8D%E9%99%90%E4%BA%8E%E5%8D%95%E6%A0%B9%E8%8A%82%E7%82%B9"><span class="toc-number">1.</span> <span class="toc-text">1.不限于单根节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-composition-API"><span class="toc-number">2.</span> <span class="toc-text">2.composition API</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-vtu"><span class="toc-number">3.</span> <span class="toc-text">3.vtu</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-v-for-%E4%B8%AD%E7%9A%84-Ref-%E6%95%B0%E7%BB%84"><span class="toc-number">4.</span> <span class="toc-text">4.v-for 中的 Ref 数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E5%BC%82%E6%AD%A5%E7%BB%84%E4%BB%B6"><span class="toc-number">5.</span> <span class="toc-text">5.异步组件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4"><span class="toc-number">6.</span> <span class="toc-text">6.自定义指令</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#2-x-%E8%AF%AD%E6%B3%95"><span class="toc-number">6.1.</span> <span class="toc-text">2.x 语法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-x-%E8%AF%AD%E6%B3%95"><span class="toc-number">6.2.</span> <span class="toc-text">3.x 语法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-data%E9%80%89%E9%A1%B9"><span class="toc-number">7.</span> <span class="toc-text">7.data选项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-%E4%BA%8B%E4%BB%B6API"><span class="toc-number">8.</span> <span class="toc-text">8.事件API</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">9.</span> <span class="toc-text">9.过滤器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-%E7%89%87%E6%AE%B5"><span class="toc-number">10.</span> <span class="toc-text">10.片段</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-%E5%85%A8%E5%B1%80API"><span class="toc-number">11.</span> <span class="toc-text">11.全局API</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E8%80%85%E9%A1%BB%E7%9F%A5"><span class="toc-number">11.1.</span> <span class="toc-text">插件使用者须知</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8C%82%E8%BD%BD-App-%E5%AE%9E%E4%BE%8B"><span class="toc-number">11.2.</span> <span class="toc-text">挂载 App 实例</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-key%E5%B1%9E%E6%80%A7"><span class="toc-number">12.</span> <span class="toc-text">12.key属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#13-%E6%8C%89%E9%94%AE%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-number">13.</span> <span class="toc-text">13.按键修饰符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#14-%E6%B8%B2%E6%9F%93%E5%87%BD%E6%95%B0API"><span class="toc-number">14.</span> <span class="toc-text">14.渲染函数API</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#2-x-%E8%AF%AD%E6%B3%95-1"><span class="toc-number">14.1.</span> <span class="toc-text">2.x 语法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-x-%E8%AF%AD%E6%B3%95-1"><span class="toc-number">14.2.</span> <span class="toc-text">3.x 语法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-%E8%BF%87%E6%B8%A1%E7%9A%84class%E5%90%8D%E6%9B%B4%E6%94%B9"><span class="toc-number">15.</span> <span class="toc-text">15.过渡的class名更改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#16-v-if-%E4%B8%8E-v-for-%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7%E5%AF%B9%E6%AF%94"><span class="toc-number">16.</span> <span class="toc-text">16.v-if 与 v-for 的优先级对比</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">vue3新特性</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-10-27</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">3.6k</span><span class="post-meta__separator">|</span><span>阅读时长: 14 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>就是一个简单的对vue3的一个新特性学习和记录</p>
<p><a target="_blank" rel="noopener" href="https://www.jb51.net/article/198200.htm">参考文章</a></p>
<p><a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/array-refs.html">官网参考</a></p>
<p><a target="_blank" rel="noopener" href="https://vue-composition-api-rfc.netlify.app/zh/api.html#setup">官方文档</a></p>
<p>如何将vue-cli创建的vue2.x项目转换为vue3的项目</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">vue add vue-next</span><br></pre></td></tr></table></figure>

<p>Performance (⽐ vue2 runtime快了2倍) </p>
<p>• Tree shaking（按需编译代码）</p>
<p>• Ts support （更优秀的Ts⽀持）</p>
<p>• Composition API（组合式 API） </p>
<p>• Custom Renderer API （⾃定义渲染器）</p>
<p>• 内置新特性组件</p>
<p>这三个是在开课吧里面介绍的几个新特性点，后面可能会重复记录</p>
<h4 id="1-不限于单根节点"><a href="#1-不限于单根节点" class="headerlink" title="1.不限于单根节点"></a>1.不限于单根节点</h4><p>即不再只能包含在一个div里面了,不再限于模板中的单个根节点</p>
<h4 id="2-composition-API"><a href="#2-composition-API" class="headerlink" title="2.composition API"></a>2.composition API</h4><p>更好的逻辑复用与代码组织，更好的类型推导，在对写大型组件的时候，可以逻辑更清晰</p>
<p>对options api的改变，对mixin的替代，将生命周期这些直接可以抽离出组件，进行封装，进行逻辑共用</p>
<p><img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201027191959787.png" alt="image-20201027191959787"></p>
<p>在调用composition api的时候，必须在vue文件的script里面调用setup( ) {}函数进行初始化,对系统的api进行函数式的调用，可基于不同的功能点进行拆分</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">&lt;p @click = &quot;handleclick&quot;&gt;</span><br><span class="line">    count:&#123;&#123;count&#125;&#125;</span><br><span class="line">    state:&#123;&#123;state.timer&#125;&#125;</span><br><span class="line">    double:&#123;&#123;double&#125;&#125;</span><br><span class="line">    &lt;/p&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    //组合式api的引入</span><br><span class="line">    import &#123;ref,reactive,readonly,computed,watch&#125; from &#x27;vue&#x27;</span><br><span class="line">    export default &#123;</span><br><span class="line">        //注意这个setup()函数，vue3的重点</span><br><span class="line">        setup()&#123;</span><br><span class="line">            //第一种响应式的方式ref	值类型string number </span><br><span class="line">            //创建一个响应式数据对象，以vue3的方式。如果传入 ref 的是一个对象，将调用 reactive 方法进行深层响应转换。</span><br><span class="line">            const count  = ref(0);	</span><br><span class="line">            const handleclick = ()=&gt;&#123;</span><br><span class="line">                count.value++;	//因为现在的count变成了一个对象了，需要控制的是其中的value</span><br><span class="line">                state.timer++;</span><br><span class="line">            &#125;</span><br><span class="line">            //第二种响应式的方式reactive	引用类型&#123;&#125; []</span><br><span class="line">            //当 ref 作为 reactive 对象的 property 被访问或修改时，也将自动解套 value 值</span><br><span class="line">            const state  = reactive(&#123;timer:0,count&#125;);	//创建出来是一个proxy对象</span><br><span class="line">            //readonly格式类似reactive，但是只能初始化进行读取，无法进行更改</span><br><span class="line">            </span><br><span class="line">            //响应式系统api,传入一个 getter 函数，返回一个默认不可手动修改的 ref 对象。</span><br><span class="line">            const double = computed(()=&gt;&#123;</span><br><span class="line">                return count.value * 2</span><br><span class="line">            &#125;);</span><br><span class="line">            watch(double,(value,prevValue)=&gt;&#123;</span><br><span class="line">                console.log(value)</span><br><span class="line">            &#125;);</span><br><span class="line">            //注意所有的变量包括函数都需要返回</span><br><span class="line">            return &#123;</span><br><span class="line">                count,//注意这里只需要传出count，不需要里面的value，它会自动解套</span><br><span class="line">                handleclick,</span><br><span class="line">                state,</span><br><span class="line">                double</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p><code>watchEffect</code></p>
<p>立即执行传入的一个函数，并响应式追踪其依赖，并在其依赖变更时重新运行该函数。可在后面显式的进行调用返回值停止监听</p>
<h4 id="3-vtu"><a href="#3-vtu" class="headerlink" title="3.vtu"></a>3.vtu</h4><p>官方指定的单元测试库，帮助我们快速测试一个组件</p>
<p><strong>测试原则</strong></p>
<p>• input<br>​    • props<br>​    • ⽤户交互<br>• output<br>​    • dom 的渲染<br>​    • Events<br>• 测试功能，⽽不是测试细节</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201027202241342.png" alt="image-20201027202241342" style="zoom:50%;" />

<p><strong>测试三部曲</strong></p>
<p>• given<br>​    • 准备测试数据<br>• when<br>​    • 触发要测试的动作<br>• then<br>​    • 验证结果</p>
<h4 id="4-v-for-中的-Ref-数组"><a href="#4-v-for-中的-Ref-数组" class="headerlink" title="4.v-for 中的 Ref 数组"></a>4.v-for 中的 Ref 数组</h4><p>在 Vue 2 中，在 <code>v-for</code> 里使用的 <code>ref</code> attribute 会用 ref 数组填充相应的 <code>$refs</code> property。当存在嵌套的 <code>v-for</code> 时，这种行为会变得不明确且效率低下。</p>
<p>在 Vue 3 中，这样的用法将不再在 <code>$ref</code> 中自动创建数组。要从单个绑定获取多个 ref，请将 <code>ref</code> 绑定到一个更灵活的函数上 (这是一个新特性)：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">v-for</span>=<span class="string">&quot;item in list&quot;</span> <span class="attr">:ref</span>=<span class="string">&quot;setItemRef&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>结合选项式 API:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">itemRefs</span>: []</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="function"><span class="title">setItemRef</span>(<span class="params">el</span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.itemRefs.push(el)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">beforeUpdate</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.itemRefs = []</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">updated</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.itemRefs)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结合组合式 API:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; ref, onBeforeUpdate, onUpdated &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> itemRefs = []</span><br><span class="line">    <span class="keyword">const</span> setItemRef = <span class="function"><span class="params">el</span> =&gt;</span> &#123;</span><br><span class="line">      itemRefs.push(el)</span><br><span class="line">    &#125;</span><br><span class="line">    onBeforeUpdate(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      itemRefs = []</span><br><span class="line">    &#125;)</span><br><span class="line">    onUpdated(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(itemRefs)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      itemRefs,</span><br><span class="line">      setItemRef</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：</p>
<ul>
<li><code>itemRefs</code> 不必是数组：它也可以是一个对象，其 ref 会通过迭代的 key 被设置。</li>
<li>如果需要，<code>itemRef</code> 也可以是响应式的且可以被监听。</li>
</ul>
<h4 id="5-异步组件"><a href="#5-异步组件" class="headerlink" title="5.异步组件"></a>5.异步组件</h4><p>以下是对变化的高层次概述：</p>
<ul>
<li>新的 <code>defineAsyncComponent</code> 助手方法，用于显式地定义异步组件</li>
<li><code>component</code> 选项重命名为 <code>loader</code></li>
<li>Loader 函数本身不再接收 <code>resolve</code> 和 <code>reject</code> 参数，且必须返回一个 Promise</li>
</ul>
<p>以前，异步组件是通过将组件定义为返回 Promise 的函数来创建的，例如：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> asyncPage = <span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./NextPage.vue&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>或者，对于带有选项的更高阶的组件语法：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> asyncPage = &#123;</span><br><span class="line">  <span class="attr">component</span>: <span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./NextPage.vue&#x27;</span>),</span><br><span class="line">  <span class="attr">delay</span>: <span class="number">200</span>,</span><br><span class="line">  <span class="attr">timeout</span>: <span class="number">3000</span>,</span><br><span class="line">  <span class="attr">error</span>: ErrorComponent,</span><br><span class="line">  <span class="attr">loading</span>: LoadingComponent</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>现在，在 Vue 3 中，由于函数式组件被定义为纯函数，因此异步组件的定义需要通过将其包装在新的 <code>defineAsyncComponent</code> 助手方法中来显式地定义：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; defineAsyncComponent &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ErrorComponent <span class="keyword">from</span> <span class="string">&#x27;./components/ErrorComponent.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> LoadingComponent <span class="keyword">from</span> <span class="string">&#x27;./components/LoadingComponent.vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 不带选项的异步组件</span></span><br><span class="line"><span class="keyword">const</span> asyncPage = defineAsyncComponent(<span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./NextPage.vue&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 带选项的异步组件</span></span><br><span class="line"><span class="keyword">const</span> asyncPageWithOptions = defineAsyncComponent(&#123;</span><br><span class="line">  <span class="comment">//注意，component 选项现在被重命名为 loader，以便准确地传达不能直接提供组件定义的信息。</span></span><br><span class="line">  <span class="attr">loader</span>: <span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./NextPage.vue&#x27;</span>),</span><br><span class="line">  <span class="attr">delay</span>: <span class="number">200</span>,</span><br><span class="line">  <span class="attr">timeout</span>: <span class="number">3000</span>,</span><br><span class="line">  <span class="attr">errorComponent</span>: ErrorComponent,</span><br><span class="line">  <span class="attr">loadingComponent</span>: LoadingComponent</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Loader 函数本身不再接收 `resolve` 和 `reject` 参数，且必须返回一个 Promise</span></span><br><span class="line"><span class="comment">// 2.x 版本</span></span><br><span class="line"><span class="keyword">const</span> oldAsyncComponent = <span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">/* ... */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3.x 版本</span></span><br><span class="line"><span class="keyword">const</span> asyncComponent = defineAsyncComponent(</span><br><span class="line">  <span class="function">() =&gt;</span></span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">/* ... */</span></span><br><span class="line">    &#125;)</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h4 id="6-自定义指令"><a href="#6-自定义指令" class="headerlink" title="6.自定义指令"></a>6.自定义指令</h4><h5 id="2-x-语法"><a href="#2-x-语法" class="headerlink" title="2.x 语法"></a>2.x 语法</h5><p>在 Vue 2，自定义指令是通过使用下面列出的钩子来创建的，这些钩子都是可选的</p>
<ul>
<li><strong>bind</strong> - 指令绑定到元素后发生。只发生一次。</li>
<li><strong>inserted</strong> - 元素插入父 DOM 后发生。</li>
<li><strong>update</strong> - 当元素更新，但子元素尚未更新时，将调用此钩子。</li>
<li><strong>componentUpdated</strong> - 一旦组件和子级被更新，就会调用这个钩子。</li>
<li><strong>unbind</strong> - 一旦指令被移除，就会调用这个钩子。也只调用一次。</li>
</ul>
<p>下面是一个例子：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-highlight</span>=<span class="string">&quot;yellow&quot;</span>&gt;</span>高亮显示此文本亮黄色<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">Vue.directive(<span class="string">&#x27;highlight&#x27;</span>, &#123;</span><br><span class="line">  <span class="function"><span class="title">bind</span>(<span class="params">el, binding, vnode</span>)</span> &#123;</span><br><span class="line">    el.style.background = binding.value</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>在这里，在这个元素的初始设置中，指令通过传递一个值来绑定样式，该值可以通过应用程序更新为不同的值。</p>
<h5 id="3-x-语法"><a href="#3-x-语法" class="headerlink" title="3.x 语法"></a>3.x 语法</h5><p>然而，在 Vue 3 中，我们为自定义指令创建了一个更具凝聚力的 API。正如你所看到的，它们与我们的组件生命周期方法有很大的不同，即使我们正与类似的事件钩子，我们现在把它们统一起来了：</p>
<ul>
<li>bind → <strong>beforeMount</strong></li>
<li>inserted → <strong>mounted</strong></li>
<li><strong>beforeUpdate</strong>：新的！这是在元素本身更新之前调用的，很像组件生命周期钩子。</li>
<li>update → 移除！有太多的相似之处要更新，所以这是多余的，请改用 <code>updated</code>。</li>
<li>componentUpdated → <strong>updated</strong></li>
<li><strong>beforeUnmount</strong>：新的！与组件生命周期钩子类似，它将在卸载元素之前调用。</li>
<li>unbind -&gt; <strong>unmounted</strong></li>
</ul>
<p>最终 API 如下：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> MyDirective = &#123;</span><br><span class="line">  <span class="function"><span class="title">beforeMount</span>(<span class="params">el, binding, vnode, prevVnode</span>)</span> &#123;&#125;,</span><br><span class="line">  <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;&#125;,</span><br><span class="line">  <span class="function"><span class="title">beforeUpdate</span>(<span class="params"></span>)</span> &#123;&#125;,</span><br><span class="line">  <span class="function"><span class="title">updated</span>(<span class="params"></span>)</span> &#123;&#125;,</span><br><span class="line">  <span class="function"><span class="title">beforeUnmount</span>(<span class="params"></span>)</span> &#123;&#125;, <span class="comment">// 新</span></span><br><span class="line">  <span class="function"><span class="title">unmounted</span>(<span class="params"></span>)</span> &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>生成的 API 可以这样使用，与前面的示例相同：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">v-highlight</span>=<span class="string">&quot;yellow&quot;</span>&gt;</span>高亮显示此文本亮黄色<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> app = Vue.createApp(&#123;&#125;)</span><br><span class="line"></span><br><span class="line">app.directive(<span class="string">&#x27;highlight&#x27;</span>, &#123;</span><br><span class="line">  <span class="function"><span class="title">beforeMount</span>(<span class="params">el, binding, vnode</span>)</span> &#123;</span><br><span class="line">    el.style.background = binding.value</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="7-data选项"><a href="#7-data选项" class="headerlink" title="7.data选项"></a>7.data选项</h4><p>在 2.x 中，开发者可以定义 <code>data</code> 选项是 <code>object</code> 或者是 <code>function</code>。</p>
<p>例如：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Object 声明 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    <span class="attr">data</span>: &#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">apiKey</span>: <span class="string">&#x27;a1b2c3&#x27;</span></span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">  &#125;)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- Function 声明 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">apiKey</span>: <span class="string">&#x27;a1b2c3&#x27;</span></span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">  &#125;)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>虽然这对于具有共享状态的根实例提供了一些便利，但是由于只有在根实例上才有可能，这导致了混乱。</p>
<p>在 3.x，<code>data</code> 选项已标准化为只接受返回 <code>object</code> 的 <code>function</code>。</p>
<p>使用上面的示例，代码只有一个可能的实现：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">  createApp(&#123;</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">apiKey</span>: <span class="string">&#x27;a1b2c3&#x27;</span></span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">  &#125;).mount(<span class="string">&#x27;#app&#x27;</span>)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>此外，当来自组件的 <code>data()</code> 及其 mixin 或 extends 基类被合并时，现在将<em>浅层次</em>执行合并：</p>
<p>在 Vue 2.x中，生成的 <code>$data</code> 是：</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  user: &#123;</span><br><span class="line">    id: <span class="number">2</span>,</span><br><span class="line">    name: &#x27;Jack&#x27;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 3.0 中，其结果将会是：</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  user: &#123;</span><br><span class="line">    id: <span class="number">2</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="8-事件API"><a href="#8-事件API" class="headerlink" title="8.事件API"></a>8.事件API</h4><p>我们整个从实例中移除了 <code>$on</code>，<code>$off</code> 和 <code>$once</code> 方法，<code>$emit</code> 仍然是现有 API 的一部分，因为它用于触发由父组件以声明方式附加的事件处理程序</p>
<h4 id="9-过滤器"><a href="#9-过滤器" class="headerlink" title="9.过滤器"></a>9.过滤器</h4><p>在 3.x 中，过滤器已删除，不再支持。相反地，我们建议用方法调用或计算属性替换它们。</p>
<h4 id="10-片段"><a href="#10-片段" class="headerlink" title="10.片段"></a>10.片段</h4><p>在 Vue 3 中，组件现在正式支持多根节点组件，即片段！</p>
<h4 id="11-全局API"><a href="#11-全局API" class="headerlink" title="11.全局API"></a>11.全局API</h4><p>从技术上讲，Vue 2 没有“app”的概念，我们定义的应用只是通过 <code>new Vue()</code> 创建的根 Vue 实例。从同一个 Vue 构造函数<strong>创建的每个根实例共享相同的全局配置</strong></p>
<p>在 Vue 3 中我们引入…   </p>
<p>一个新的全局 API：<code>createApp</code></p>
<p>调用 <code>createApp</code> 返回一个应用实例，这是 Vue 3 中的新概念：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = createApp(&#123;&#125;)</span><br></pre></td></tr></table></figure>

<p>应用实例暴露当前全局 API 的子集，经验法则是，任何全局改变 Vue 行为的 API 现在都会移动到应用实例上，以下是当前全局 API 及其相应实例 API 的表：</p>
<table>
<thead>
<tr>
<th>2.x 全局 API</th>
<th>3.x 实例 API (<code>app</code>)</th>
</tr>
</thead>
<tbody><tr>
<td>Vue.config</td>
<td>app.config</td>
</tr>
<tr>
<td>Vue.config.productionTip</td>
<td><em>removed</em> (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#config-productiontip-removed">见下方</a>)</td>
</tr>
<tr>
<td>Vue.config.ignoredElements</td>
<td>app.config.isCustomElement (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#config-ignoredelements-is-now-config-iscustomelement">见下方</a>)</td>
</tr>
<tr>
<td>Vue.component</td>
<td>app.component</td>
</tr>
<tr>
<td>Vue.directive</td>
<td>app.directive</td>
</tr>
<tr>
<td>Vue.mixin</td>
<td>app.mixin</td>
</tr>
<tr>
<td>Vue.use</td>
<td>app.use (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#a-note-for-plugin-authors">见下方</a>)</td>
</tr>
</tbody></table>
<p>所有其他不全局改变行为的全局 API 现在被命名为 exports，文档见<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api-treeshaking.html">全局 API Treeshaking</a></p>
<h5 id="插件使用者须知"><a href="#插件使用者须知" class="headerlink" title="插件使用者须知"></a>插件使用者须知</h5><p>插件开发者通常使用 <code>Vue.use</code>。例如，官方的 <code>vue-router</code> 插件是如何在浏览器环境中自行安装的：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> inBrowser = <span class="keyword">typeof</span> <span class="built_in">window</span> !== <span class="string">&#x27;undefined&#x27;</span></span><br><span class="line"><span class="comment">/* … */</span></span><br><span class="line"><span class="keyword">if</span> (inBrowser &amp;&amp; <span class="built_in">window</span>.Vue) &#123;</span><br><span class="line">  <span class="built_in">window</span>.Vue.use(VueRouter)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由于 <code>use</code> 全局 API 在 Vue 3 中不再使用，此方法将停止工作并停止调用 <code>Vue.use()</code> 现在将触发警告，于是，开发者必须在应用程序实例上显式指定使用此插件：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> app = createApp(MyApp)</span><br><span class="line">app.use(VueRouter)</span><br></pre></td></tr></table></figure>



<h5 id="挂载-App-实例"><a href="#挂载-App-实例" class="headerlink" title="挂载 App 实例"></a>挂载 App 实例</h5><p>使用 <code>createApp(/* options */)</code> 初始化后，应用实例 <code>app</code> 可用于挂载具有 <code>app.mount(domTarget)</code>：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> MyApp <span class="keyword">from</span> <span class="string">&#x27;./MyApp.vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = createApp(MyApp)</span><br><span class="line">app.mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>经过所有这些更改，我们在指南开头的组件和指令将被改写为如下内容：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> app = createApp(MyApp)</span><br><span class="line"></span><br><span class="line">app.component(<span class="string">&#x27;button-counter&#x27;</span>, &#123;</span><br><span class="line">  <span class="attr">data</span>: <span class="function">() =&gt;</span> (&#123;</span><br><span class="line">    <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line">  &#125;),</span><br><span class="line">  <span class="attr">template</span>: <span class="string">&#x27;&lt;button @click=&quot;count++&quot;&gt;Clicked &#123;&#123; count &#125;&#125; times.&lt;/button&gt;&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.directive(<span class="string">&#x27;focus&#x27;</span>, &#123;</span><br><span class="line">  <span class="attr">mounted</span>: <span class="function"><span class="params">el</span> =&gt;</span> el.focus()</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 现在所有应用实例都挂载了，与其组件树一起，将具有相同的 “button-counter” 组件 和 “focus” 指令不污染全局环境</span></span><br><span class="line">app.mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="12-key属性"><a href="#12-key属性" class="headerlink" title="12.key属性"></a>12.key属性</h4><ul>
<li>新增：对于 v-if/v-else/v-else-if的各分支项key将不再是必须的，因为现在 Vue 会自动生成唯一的key<ul>
<li><strong>非兼容</strong>：如果你手动提供 <code>key</code>，那么每个分支必须使用唯一的 <code>key</code>。你不能通过故意使用相同的 <code>key</code> 来强制重用分支。</li>
</ul>
</li>
<li><strong>非兼容</strong>：<code>&lt;template v-for&gt;</code> 的 <code>key</code> 应该设置在 <code>&lt;template&gt;</code> 标签上 (而不是设置在它的子节点上)。</li>
</ul>
<p>在 Vue 2.x 中 <code>&lt;template&gt;</code> 标签不能拥有 <code>key</code>。不过你可以为其每个子节点分别设置 <code>key</code>。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Vue 2.x --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">v-for</span>=<span class="string">&quot;item in list&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span>&gt;</span>...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span>&gt;</span>...<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在 Vue 3.x 中 <code>key</code> 则应该被设置在 <code>&lt;template&gt;</code> 标签上。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Vue 3.x --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">v-for</span>=<span class="string">&quot;item in list&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span>&gt;</span>...<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="13-按键修饰符"><a href="#13-按键修饰符" class="headerlink" title="13.按键修饰符"></a>13.按键修饰符</h4><ul>
<li><strong>非兼容</strong>：不再支持使用数字 (即键码) 作为 <code>v-on</code> 修饰符</li>
<li><strong>非兼容</strong>：不再支持 <code>config.keyCodes</code></li>
</ul>
<p>在 Vue 2 中，支持 <code>keyCodes</code> 作为修改 <code>v-on</code> 方法的方法。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 键码版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-on:keyup.13</span>=<span class="string">&quot;submit&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 别名版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-on:keyup.enter</span>=<span class="string">&quot;submit&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>此外，你可以通过全局 <code>config.keyCodes</code> 选项。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">Vue.config.keyCodes = &#123;</span><br><span class="line">  <span class="attr">f1</span>: <span class="number">112</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 键码版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-on:keyup.112</span>=<span class="string">&quot;showHelpText&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 自定别名版本 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-on:keyup.f1</span>=<span class="string">&quot;showHelpText&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>



<p>从<a target="_blank" rel="noopener" href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode"><code>KeyboardEvent.keyCode</code> has been deprecated</a> 开始，Vue 3 继续支持这一点就不再有意义了。因此，现在建议对任何要用作修饰符的键使用 kebab-cased (短横线) 大小写名称。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Vue 3 在 v-on 上使用 按键修饰符 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-on:keyup.delete</span>=<span class="string">&quot;confirmDelete&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>因此，这意味着 <code>config.keyCodes</code> 现在也已弃用，不再受支持。</p>
<h4 id="14-渲染函数API"><a href="#14-渲染函数API" class="headerlink" title="14.渲染函数API"></a>14.渲染函数API</h4><h5 id="2-x-语法-1"><a href="#2-x-语法-1" class="headerlink" title="2.x 语法"></a>2.x 语法</h5><p>在 2.x 中，e <code>render</code> 函数将自动接收 <code>h</code> 函数 (它是 <code>createElement</code> 的常规别名) 作为参数：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// Vue 2 渲染函数示例</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params">h</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> h(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="3-x-语法-1"><a href="#3-x-语法-1" class="headerlink" title="3.x 语法"></a>3.x 语法</h5><p>在 3.x 中，<code>h</code> 现在是全局导入的，而不是作为参数自动传递。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// Vue 3 渲染函数示例</span></span><br><span class="line"><span class="keyword">import</span> &#123; h &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> h(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="15-过渡的class名更改"><a href="#15-过渡的class名更改" class="headerlink" title="15.过渡的class名更改"></a>15.过渡的class名更改</h4><p>过渡类名 <code>v-enter</code> 修改为 <code>v-enter-from</code>、过渡类名 <code>v-leave</code> 修改为 <code>v-leave-from</code>。</p>
<p>为了更加明确易读，我们现在将这些初始状态重命名为：</p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-class">.v-enter-from</span>,</span><br><span class="line"><span class="selector-class">.v-leave-to</span> &#123;</span><br><span class="line">  <span class="attribute">opacity</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.v-leave-from</span>,</span><br><span class="line"><span class="selector-class">.v-enter-to</span> &#123;</span><br><span class="line">  <span class="attribute">opacity</span>: <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在，这些状态之间的区别就清晰多了。</p>
<p><code>&lt;transition&gt;</code> 组件相关属性名也发生了变化：</p>
<ul>
<li><code>leave-class</code> 已经被重命名为 <code>leave-from-class</code> (在渲染函数或 JSX 中可以写为：<code>leaveFromClass</code>)</li>
<li><code>enter-class</code> 已经被重命名为 <code>enter-from-class</code> (在渲染函数或 JSX 中可以写为：<code>enterFromClass</code>)</li>
</ul>
<h4 id="16-v-if-与-v-for-的优先级对比"><a href="#16-v-if-与-v-for-的优先级对比" class="headerlink" title="16.v-if 与 v-for 的优先级对比"></a>16.v-if 与 v-for 的优先级对比</h4><p>2.x 版本中在一个元素上同时使用 <code>v-if</code> 和 <code>v-for</code> 时，<code>v-for</code> 会优先作用。</p>
<p>3.x 版本中 <code>v-if</code> 总是优先于 <code>v-for</code> 生效</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/10/27/vue3新特性/">https://chengsong.info/2020/10/27/vue3新特性/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/vue3/">vue3</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/10/29/vue3%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/"><i class="fa fa-chevron-left">  </i><span>vue3简单使用</span></a></div><div class="next-post pull-right"><a href="/2020/10/25/uniapp%E5%88%9D%E5%AD%A6%E4%B9%A0/"><span>uniapp初学习</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>